package com.yang.studentsapp.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.yang.studentsapp.BaseSociety;
import com.yang.studentsapp.CampusSociety;
import com.yang.studentsapp.MessageType;
import com.yang.studentsapp.SocietyBigPic;
import com.yang.studentsapp.SocietySmallPic;
import com.yang.studentsapp.SocietyText;
import com.yang.studentsapp.SocietyThirdPic;

import java.util.List;

/**
 * Created by ${Ян-Вэнь-Чао} on 2016/10/13.
 */
public class SocietyAdapter extends CommonBaseAdapter<CampusSociety> {

    public SocietyAdapter(Context context, List<CampusSociety> dataList) {
        super(context, dataList);
    }
    @Override
    public int getItemViewType(int position) {
        CampusSociety society = new CampusSociety();
        if(society != null){
            if(society.getNewsType() == null){
                return 0;
            }else {
                return type(society.getNewsType());
            }
        }
        return 0;
    }

    private int type(String a){
        switch (a) {
            case MessageType.tvType:
                return 0;
            case MessageType.smallPicType:
                return 1;
            case MessageType.bigPicType:
                return 2;
            case MessageType.thirdPicType:
                return 3;
        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseSociety society;
        if(convertView != null){
            society = (BaseSociety) convertView.getTag();
        } else {
            society = createSociety((CampusSociety) getItem(position),position);
            convertView = society.getConvertView();
        }
        society.setPosition(position);
//        society.setSociety(getItem(position));
        society.setSociety((CampusSociety) getItem(position));
        society.setData((CampusSociety) getItem(position));
        return convertView;
    }
    private BaseSociety createSociety(CampusSociety item, int position) {
        BaseSociety society = null;
        int viewType = getItemViewType(position);
        switch (viewType) {
            case 0:
                society = new SocietyText(item,mContext);
                break;
            case 1:
                society = new SocietySmallPic(item,mContext);
                break;
            case 2:
                society = new SocietyBigPic(item,mContext);
                break;
            case 3:
                society = new SocietyThirdPic(item,mContext);
                break;
        }
        return society;
    }
}
